package avicit.bdp.dms.das.rest;

import avicit.bdp.common.service.dto.CommonTypeDTO;
import avicit.bdp.common.service.service.CommonTypeService;
import avicit.bdp.common.service.service.DataSourceService;
import avicit.platform6.core.context.ThreadContextHelper;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 通用树Rest接口
 */
@RestController
@Api(tags = "通用树Rest接口")
@RequestMapping("/api/bdp/dms/das/commontype")
public class BdpDasCommonTypeRest {

    private static final Logger LOGGER = LoggerFactory.getLogger(BdpDasCommonTypeRest.class);

    @Autowired
    private CommonTypeService bdpDasCommonTypeService;

    @Autowired
    private DataSourceService dataSourceService;

    /**
     * 新增通用分类对象
     */
    @ApiOperation(value = "新增对象")
    @PostMapping("/insertCommonType")
    public ResponseMsg<String> insertCommonType(@RequestBody CommonTypeDTO bdpDasCommonType) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        bdpDasCommonType.setOrgIdentity(ThreadContextHelper.getOrgId());
        responseMsg.setResponseBody(bdpDasCommonTypeService.insertBdpDasCommonType(bdpDasCommonType));
        return responseMsg;
    }


    /**
     * 修改通用分类对象
     */
    @ApiOperation(value = "修改对象")
    @PostMapping("/updateCommonType")
    public ResponseMsg<String> updateCommonType(@RequestBody CommonTypeDTO bdpDasCommonType) {
        ResponseMsg<String> responseMsg = new ResponseMsg<>();
        bdpDasCommonTypeService.updateBdpDasCommonType(bdpDasCommonType);
        return responseMsg;
    }

    /**
     * 删除通用分类对象
     */
    @ApiOperation(value = "按主键单条删除")
    @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "body", required = true)
    @PostMapping("/deleteCommonType")
    public ResponseMsg<Integer> deleteById(@ApiIgnore @RequestBody Map queryMap) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        String id = (String) queryMap.get("id");
        bdpDasCommonTypeService.deleteBdpDasCommonTypeById(id);
        return responseMsg;
    }


    /**
     * 查询通用树结构（同步）
     */
    @ApiOperation(value = "查询树结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "分类类型", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目空间ID", dataType = "String")
    })
    @GetMapping("/findCommonType")
    public ResponseMsg<List<CommonTypeDTO>> findCommonType(@RequestParam String type,
                                                           @RequestParam(required = false) String projectId) {
        ResponseMsg<List<CommonTypeDTO>> responseMsg = new ResponseMsg<>();
        List<CommonTypeDTO> result = bdpDasCommonTypeService.findCommonType(type, projectId);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 查询数据源分类树
     * 注意：
     * 1、仅在用于数据源授权时获取分类树
     */
    @ApiOperation(value = "查询树结构")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "分类类型", dataType = "String"),
            @ApiImplicitParam(name = "keyWords", value = "关键字", dataType = "String")
    })
    @GetMapping("/findDsCommonType")
    public ResponseMsg<List<CommonTypeDTO>> findDsCommonType(@RequestParam String type,
                                                             @RequestParam(required = false) String keyWords) {
        ResponseMsg<List<CommonTypeDTO>> responseMsg = new ResponseMsg<>();
        List<CommonTypeDTO> result = bdpDasCommonTypeService.findDsCommonType(type, keyWords);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

    /**
     * 判断分类节点名是否重复
     */
    @ApiOperation(value = "判断名称是否重复")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "分类id", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "name", value = "分类名称", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "parentId", value = "分类父id", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "type", value = "分类类型", dataType = "String", paramType = "body"),
            @ApiImplicitParam(name = "projectId", value = "项目空间id", dataType = "String", paramType = "body")
    })
    @PostMapping("/isSameName")
    public ResponseMsg<Integer> isSameName(@ApiIgnore @RequestBody Map queryMap) {
        ResponseMsg<Integer> responseMsg = new ResponseMsg<>();
        String typeId = MapUtils.getString(queryMap, "typeId", "");
        String name = MapUtils.getString(queryMap, "name", "");
        String parentId = MapUtils.getString(queryMap, "parentId", "");
        String type = MapUtils.getString(queryMap, "type", "");
        String projectId = MapUtils.getString(queryMap, "projectId", "");
        Integer flag = bdpDasCommonTypeService.isSameName(typeId, name, parentId, type, projectId);
        responseMsg.setResponseBody(flag);
        return responseMsg;
    }


    /**
     * 获取数据源列表。BdpDasCommonTypeRest中调用
     * 注意：
     * 1、数据建模-模型导入时，获取数据源列表
     * 2、流程设计，相关节点中获取数据源列表，例如数据同步、数据接入、数据SQL加工等
     */
    @ApiOperation(value = "数据源展示列表")
    @ApiImplicitParam(name = "projectId", value = "数据空间id", dataType = "String")
    @GetMapping("/findDatasourceList")
    public ResponseMsg<List<CommonTypeDTO>> findDatasourceList(@RequestParam(required = false) String projectId) {
        ResponseMsg<List<CommonTypeDTO>> responseMsg = new ResponseMsg<>();
        List<CommonTypeDTO> result = dataSourceService.findProDatasourceList(projectId);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }


    /**
     * 数据工作台中获取落库数据源列表
     */
    @ApiOperation(value = "数据空间数据源展示列表")
    @ApiImplicitParam(name = "projectId", value = "数据空间id", dataType = "String")
    @GetMapping("/findProDatasourceList")
    public ResponseMsg<List<CommonTypeDTO>> findProDatasourceList(@RequestParam(required = false) String projectId) {
        ResponseMsg<List<CommonTypeDTO>> responseMsg = new ResponseMsg<>();
        List<CommonTypeDTO> result = dataSourceService.findProDatasourceList(projectId);
        responseMsg.setResponseBody(result);
        return responseMsg;
    }

}
